package org.example.multiThreading.threadArray;

import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestCopyOnWriteArraySet {
    public static void main(String[] args) {
        CopyOnWriteArraySet<Integer> arraySet = new CopyOnWriteArraySet<>();
        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(5);
        //创建十个线程，并向里面添加值
        for (int i = 0; i < 10; i++) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (int i1 = 0; i1 < 1000; i1++) {
                        arraySet.add(i1);
                    }
                }
            };
            //加入线程池
            service.submit(runnable);
        }
        //关闭线程池
        service.shutdown();

        //检测线程池是否关闭
        while (!service.isTerminated()){}

        for (Integer integer : arraySet) {
            System.out.println(integer);
        }
    }
}
